package com.android.clinic.doctors.app.ui.patient;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.entity.ReplyPatientInfo;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.utils.Utils;
import com.jiongbull.jlog.JLog;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 给患者回复
 * Created by cheng on 2018/6/12.
 */
public class ReplyPatientActivity extends BaseActivity {

    private EditText etContent;
    private String userId = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_reply_patient;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        String nickName = this.getIntent().getStringExtra("name");
        if (!Utils.isEmpty(nickName)) {
            setTitleBarTitle(nickName);
        } else {
            setTitleBarTitle("--");
        }
        userId = this.getIntent().getStringExtra("userId");
        etContent = findViewById(R.id.et_content);
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_submit) {
            if (etContent.getText().toString().trim().length() == 0) {
                showToast("请输入内容!");
                return;
            }
            doctorContactPatient(etContent.getText().toString());
        }
    }

    /**
     * 医生联系患者
     * @param content
     */
    private void doctorContactPatient(String content) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("connectTime", System.currentTimeMillis());
            jsonObject.put("content", content);
            jsonObject.put("userId", userId);
            jsonObject.put("doctorId", ClinicDoctorsApplication.getUserInfo().getId());
            jsonObject.put("name", ClinicDoctorsApplication.getUserInfo().getName());

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());

            JLog.e("body:  " + jsonObject.toString());

            HttpClient.getHttpService().doctorContactPatient(body)
                    .compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers(loading, "正在提交..."))
                    .subscribe(new BaseObserver<ReplyPatientInfo>() {
                        @Override
                        public void onSuccess(ReplyPatientInfo replyPatientInfo) {
                            if (!Utils.isEmpty(replyPatientInfo.getDoctorId())) {
                                showToast("发送成功!");
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                            finish();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
